<?php

class ConfigArea_Model extends Model {

    public function __construct(){
        parent::__construct();
    }

    private function getArea ($idScheme, $idArea) {
        if (isset ($idScheme) && isset ($idArea)) {
            $query = "SELECT a.id, a.name, a.order FROM area a, scheme_area sa, scheme s ";
            $query .= "WHERE a.id = sa.id_area AND s.id = sa.id_scheme AND ";
            $query .= "a.id = $idArea AND s.id = $idScheme";
            return $this->_db->select ($query);
        }
        return NULL;
    }
    
    public function getAttributes ($idScheme, $idArea) {
        $result = Array ();
        
        if ($this->getArea ($idScheme, $idArea) == NULL) {
            return $result;
        }
        
        $query = "SELECT t.* FROM ";
        $query .= "( ";
        $query .= "SELECT at.id, at.name, at.order, aa.active ";
        $query .= "FROM area a, attribute at, area_attribute aa ";
        $query .= "WHERE a.id = aa.id_area AND at.id = aa.id_attribute AND a.id = $idArea ";
        $query .= "UNION ";
        $query .= "SELECT at.id, at.name, at.order, 0 FROM attribute at ";
        $query .= ") t ";
        $query .= "GROUP BY t.id ";
        $query .= "ORDER BY t.name ";
        return $this->_db->select ($query);
    }
    
    public function setAreaAttribute ($idScheme, $idArea, $idAttribute, $active) {
        if ($this->getArea ($idScheme, $idArea) == NULL) {
            return false;
        }
        
        if (isset ($idAttribute)) {
            $query = "SELECT * FROM area a, scheme_area sa, scheme s ";
            $query .= "WHERE a.id = sa.id_area AND s.id = sa.id_scheme AND ";
            $query .= "a.id = $idArea AND s.id = $idScheme";
            $res = $this->_db->select ($query);
            
            if (count ($res) > 0) {
                $query = "SELECT aa.id FROM area a, area_attribute aa, attribute at ";
                $query .= "WHERE a.id = aa.id_area AND at.id = aa.id_attribute AND ";
                $query .= "a.id = $idArea AND at.id = $idAttribute";
                $res = $this->_db->select ($query);
                
                $params = Array();
                $params['id_attribute'] = $idAttribute;
                $params['id_area'] = $idArea;
                $params['active'] = ($active == 0 ? 0 : 1);
                    
                if (count ($res) == 0) {
                    $this->_db->insert ('area_attribute', $params);
                }
                else {
                    $where = Array();
                    $where['id'] = $res[0]->id;
                    $this->_db->update ('area_attribute', $params, $where);
                }
                
                return true;
            }
        }
        return false;
    }
    
    public function getConfigurations ($idScheme, $idArea) {
        $configurations = Array ();
        
        if (is_numeric ($idScheme) && is_numeric ($idArea)) {
            $query = "SELECT c.id, c.name, c.order ";
            $query .= "FROM scheme as s, area as a, configuration as c, scheme_area as sa, area_configuration as ac ";
            $query .= "WHERE s.id = $idScheme AND a.id = $idArea AND ";
            $query .= "s.id = sa.id_scheme AND a.id = sa.id_area AND ac.id_area = a.id ";
            $query .= "GROUP BY c.id";
            $result = $this->_db->select ($query);
            
            foreach ($result as $value) {
                $configurations[] = Array (
                    "id" => $value->id,
                    "name" => $value->name,
                    "order" => $value->order
                );
            }
        }

        return $configurations;
    }
    
    public function addConfiguration ($name, $order, $idArea) {
        $params = Array();
        $params['name'] = $name;
        $params['order'] = $order;
        $id = $this->_db->insert ('configuration', $params);
        
        $params2 = Array();
        $params2['id_area'] = $idArea;
        $params2['id_configuration'] = $id;
        $this->_db->insert ('area_configuration', $params2);
        
        return true;
    }
    
    public function modifyConfiguration ($id, $name, $order) {
        $params = Array();
        $where = Array();
        $params['name'] = $name;
        $params['order'] = $order;
        $where['id'] = $id;
        $this->_db->update ('configuration', $params, $where);
        
        return true;
    }
    
    public function removeConfiguration ($id) {
        $this->_db->delete ('configuration', 'id = ' . $id);
        
        return true;
    }
}

?>